package com.hongfeng.support.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.item.tag.LogicOperator;
import com.hongfeng.support.model.Product;
import com.hongfeng.support.service.ProductService;
import com.hongfeng.support.util.config.PropertiesUtil;
import com.hongfeng.support.util.file.FileUploadUtil;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 汽车配件Controller
 * 
 * @author llc
 * 
 */
@Controller
@Online
public class ProductController extends BaseController {

	@Resource
	private ProductService productService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			Long autoBrandId, Long autoTypeId, String name, Page<Product> page) {
		log.info("查询配件名：" + name + "；所属型号ID：" + autoTypeId + "；所属品牌ID："
				+ autoBrandId);
		List<ParmItem> parms = new ArrayList<ParmItem>(3);
		parms.add(new ParmItem("autoType.autoBrand.id", autoBrandId,
				FieldOperator.EQUAL, LogicOperator.AND));
		parms.add(new ParmItem("autoType.id", autoTypeId, FieldOperator.EQUAL,
				LogicOperator.AND));
		parms.add(new ParmItem("name", name, FieldOperator.LIKE,
				LogicOperator.AND));
		
		productService.findPageByQuery(page, Product.class, parms);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("listAll")
	public void listAll(HttpServletRequest request, HttpServletResponse response) {
		List<Product> products = productService.findAll();
		String json = JsonUtils.array2Json(products);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("product") String productJson) {
		log.info("添加配件:" + (productJson != null ? productJson : null));
		if (productJson == null) {
			this.sendErrorResponse(response, "配件信息不能为空。");
			return;
		}
		Product product = (Product) JsonUtils.json2Object(productJson,
				Product.class);
		ParmItem item = new ParmItem("name", product.getName(),
				FieldOperator.EQUAL);
		List<Product> products = productService.findAllByQuery(Product.class,
				item);
		if (products.size() == 0) {
			productService.create(product);
		} else {
			this.sendErrorResponse(response, "配件名已经存在。");
		}
	}

	@RequestMapping("upload")
	public void upload(HttpServletRequest request, HttpServletResponse response) {
		String base = "product";
		String webUrl = PropertiesUtil.getConfig("hongfeng.web.url");
		try {
			List<String> files = FileUploadUtil.fileUpload(request, base);
			String fileUrl = "";
			for (String file : files) {
				fileUrl = webUrl + file;
				log.info("fileUrl = " + fileUrl);
			}
			this.sendSuccResponse(response, fileUrl);
		} catch (Exception e) {
			log.error("上传图片失败", e);
			this.sendErrorResponse(response, "上传图片失败。");
		}
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("product") String productJson) {
		log.info("修改配件:" + (productJson != null ? productJson : null));
		if (productJson == null) {
			this.sendErrorResponse(response, "配件信息不能为空。");
			return;
		}
		Product product = (Product) JsonUtils.json2Object(productJson,
				Product.class);
		Product qProduct = productService.read(product.getId());
		List<Product> products = null;
		if (!product.getName().equals(qProduct.getName())) {
			ParmItem item = new ParmItem("name", product.getName(),
					FieldOperator.EQUAL);
			products = productService.findAllByQuery(Product.class, item);
		}
		if (products == null || products.size() == 0) {
			product.setCreateTime(qProduct.getCreateTime());
			product.setCreator(qProduct.getCreator());
			productService.update(product);
		} else {
			this.sendErrorResponse(response, "配件名已经存在。");
		}
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			Long id) {
		log.info("读取配件:" + id);
		Product product = productService.read(id);
		String json = JsonUtils.bean2Json(product);
		log.info("配件信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		log.info("删除配件:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的配件。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			productService.delete(Long.parseLong(id));
		}
	}

}